package com.wgchao.authmanager.application.controller;

import com.wgchao.authmanager.application.filter.AuthMethod;
import com.wgchao.authmanager.application.filter.Authentication;
import com.wgchao.authmanager.application.service.AssignService;
import com.wgchao.authmanager.application.service.AuthorityService;
import com.wgchao.authmanager.application.vo.AuthoInfoVo;
import com.wgchao.authmanager.domain.params.AssignInfo;
import com.wgchao.authmanager.domain.params.LoginInfo;
import com.wgchao.authmanager.infrastructure.resp.BackResult;
import com.wgchao.authmanager.query.AuthoQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: LoginController
 * @author: wgchao
 * @createTime: 2020/5/9 3:37 PM
 * @Description: 权限管理
 */
@RestController
@RequestMapping("/aum/front/authority")
@Api(value = "/aum/front/authority", description = "权限管理")
public class AuthorityController {
    @Autowired
    private AuthorityService authorityService;
    @Autowired
    private AssignService assignService;
    @Autowired
    private AuthoQuery authoQuery;

    /**
     * 权限前台申请token
     * @param loginInfo
     * @return
     */
    @PostMapping("/accessToken")
    @ApiOperation(value = "申请token")
    public BackResult<String> accessToken(@RequestBody LoginInfo loginInfo){
        String accessToken = authorityService.accessToken(loginInfo, "local");
        return BackResult.success().setData(accessToken);
    }

    @PostMapping("/admin/autho/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "账户授权菜单权限", notes = "身份验证")
    public BackResult assignAuthosToAdmin(@ApiParam(value = "账户名（url参数）", name = "uname", required = true)
                                          @PathVariable String uname,
                                          @ApiParam(value = "授权信息", name = "menuUpdator", required = true)
                                          @RequestBody List<AssignInfo> assignInfos){
        assignService.assignAuthosToAdmin(uname, assignInfos, "admin");
        return BackResult.success();
    }

    @PostMapping("/role/autho/{agencyCode}/{roleName}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "角色授权菜单权限", notes = "身份验证")
    public BackResult assignAuthosToARole(@ApiParam(value = "机构编号（url参数）", name = "agencyCode", required = true)
                                          @PathVariable String agencyCode,
                                          @ApiParam(value = "角色名（url参数）", name = "roleName", required = true)
                                          @PathVariable String roleName,
                                          @ApiParam(value = "授权信息", name = "menuUpdator", required = true)
                                          @RequestBody List<AssignInfo> assignInfos){
        assignService.assignAuthosToRole(agencyCode, roleName, assignInfos, "admin");
        return BackResult.success();
    }

    @GetMapping("/role/autho/{agencyCode}/{roleName}/{projectIdentify}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "获取角色在项目下菜单权限", notes = "身份验证")
    public BackResult<List<AuthoInfoVo>> findAuthosOfRole(@ApiParam(value = "机构编号（url参数）", name = "agencyCode", required = true)
                                       @PathVariable String agencyCode,
                                       @ApiParam(value = "角色名（url参数）", name = "roleName", required = true)
                                       @PathVariable String roleName,
                                       @ApiParam(value = "项目标识（url参数）", name = "projectIdentify", required = true)
                                       @PathVariable String projectIdentify){
        List<AuthoInfoVo> authoInfoVos = authoQuery.searchAuthosOfRole(agencyCode, roleName, projectIdentify);
        return BackResult.success().setData(authoInfoVos);
    }

    @GetMapping("/admin/autho/{uname}/{projectIdentify}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "获取账户在项目下分配菜单权限", notes = "身份验证")
    public BackResult<List<AuthoInfoVo>> findAuthosOfAdmin(@ApiParam(value = "账户名（url参数）", name = "uname", required = true)
                                                          @PathVariable String uname,
                                                          @ApiParam(value = "项目标识（url参数）", name = "projectIdentify", required = true)
                                                          @PathVariable String projectIdentify){
        List<AuthoInfoVo> authoInfoVos = authoQuery.searchAuthosOfAdmin(uname, projectIdentify);
        return BackResult.success().setData(authoInfoVos);
    }

    @GetMapping("/admin/autho/all/{uname}/{projectIdentify}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "获取账户在项目下所有菜单权限", notes = "身份验证")
    public BackResult<List<AuthoInfoVo>> findAllAuthosOfAdmin(@ApiParam(value = "账户名（url参数）", name = "uname", required = true)
                                                           @PathVariable String uname,
                                                           @ApiParam(value = "项目标识（url参数）", name = "projectIdentify", required = true)
                                                           @PathVariable String projectIdentify){
        List<AuthoInfoVo> authoInfoVos = authoQuery.searchAllAuthosOfAdmin(uname, projectIdentify);
        return BackResult.success().setData(authoInfoVos);
    }
}
